#!/usr/bin/python
# -*- coding: utf8 -*-
import os,sys
if os.sys.platform.startswith("win"):
    import _winreg
else:
    print "You're not on windows ! Damn it !"
    sys.exit(1)

from xml.etree.cElementTree import ElementTree
from xml.dom.minidom import parseString
from magnetools import choosewithnumber

#Listing all MSN users...
key = _winreg.OpenKey( _winreg.HKEY_CURRENT_USER, 'Software\\Microsoft\\MSNMessenger\\PerPassportSettings',0, _winreg.KEY_READ)
i = 0
users={}
try:
    while True:
        users.update({_winreg.EnumKey(key,i): ""})
        i+=1
except EnvironmentError:
    pass
_winreg.CloseKey(key)
for userid in users.keys():
    key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Software\\Microsoft\\MSNMessenger\\PerPassportSettings' + "\\" + userid,0, _winreg.KEY_READ)
    try:
        dom = parseString(_winreg.QueryValueEx(key,'UTL')[0].replace(chr(0),""))
    except WindowsError:
        _winreg.CloseKey(key)
        #Si on ne trouve pas la clé UTL on efface l'userid du dictionnaire d'utilisateur
        users.pop(userid)
        continue
    try:
        dir=_winreg.QueryValueEx(key,'MessageLogPath')[0]
    except WindowsError:
        #log is not enabled for this user !
        users.pop(userid)
        continue
    users.update({userid: (dom.firstChild.attributes["Creator"].value,dir)})
    #stripping chr(0) from the value
    #dom = parseString(_winreg.QueryValueEx(key,'UTL')[0][::2])
    #unusable beacause the last chr(0) fuck up the code.
    _winreg.CloseKey(key)
user=choosewithnumber(users.values(),"Choisissez un contact")
#directory contient le répertoire des logs de windows live messenger
#contactdir=chosedirwithnumber(os.path.join(directory,dir,final),"Choisissez le contact",['MessageLog.xsl'])
#content = ElementTree(file=os.path.join(directory,dir,final,contactdir))